package com.iflytek.xse;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.iflytek.xse.databinding.ActivityMainBinding;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {


    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // Example of a call to a native method
//        TextView tv = binding.sampleText;

        SEManager seManager = new SEManager();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    seManager.setCallBack(new SEManager.WakeUp() {
                        @Override
                        public void onWakeUp(String data) {
                              Log.e( "ivw", "———————————————" + data);
                        }

                        @Override
                        public void onStart(byte[] data) {

                        }

                        @Override
                        public void onRec(byte[] data) {
//                            Log.e("semanager","on rec " +data.length);
                        }

                        @Override
                        public void onEnd() {

                        }

                        @Override
                        public void onSeData(byte[] data) {
//                            Log.e("semanager","onSeData rec " +data.length);
                        }
                    });
                    seManager.create("8b415468",getFilesDir().getAbsolutePath());
                    InputStream fileInputStream  = getAssets().open("pcm/raw3.pcm");
                    FileOutputStream outputStream  = new FileOutputStream(getFilesDir().getAbsolutePath()  + "/out.pcm");
//                    fileInputStream.skip(44);
                    byte[] data = new byte[1024*4];
                    while (fileInputStream.read(data) == 1024*4){
                         seManager.writeData(data);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    protected static void copyAssetsDir2Phone(Context activity, String filePath) {
        try {
            String[] fileList = activity.getAssets().list(filePath);
            if (fileList.length > 0) {//如果是目录
                File file = new File(activity.getFilesDir().getAbsolutePath() + File.separator + filePath);
                file.mkdirs();//如果文件夹不存在，则递归
                for (String fileName : fileList) {
                    filePath = filePath + File.separator + fileName;

                    copyAssetsDir2Phone(activity, filePath);

                    filePath = filePath.substring(0, filePath.lastIndexOf(File.separator));
                }
            } else {//如果是文件
                InputStream inputStream = activity.getAssets().open(filePath);
                File file = new File(activity.getFilesDir().getAbsolutePath() + File.separator + filePath);

                FileOutputStream fos = new FileOutputStream(file);
                int len = -1;
                byte[] buffer = new byte[1024];
                while ((len = inputStream.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.flush();
                inputStream.close();
                fos.close();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}